Imports TaxonNamesInterface

Public Class ReferenceSearch

    Public MaxResults As Integer = 201

    Public Event ReferenceSelected(ByVal item As ResultItem)

    Public ReadOnly Property SelectedReference() As ResultItem
        Get
            Return ResultsListBox.SelectedItem
        End Get
    End Property

    Public Sub SelectReference(ByVal refGuid As Guid)
        Try
            Dim ref As TaxonNamesInterface.Reference = NamesClient.ReferencesInterfaceProxy.GetReference(refGuid)
            Dim rr As New ResultItem
            rr.Id = refGuid
            rr.Text = ref.Citation
            ResultsListBox.Items.Clear()
            ResultsListBox.Items.Add(rr)
            ResultsListBox.SelectedIndex = 0
        Catch ex As Exception
            NamesException.LogError(ex)
        End Try
    End Sub

    Private Sub SearchButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SearchButton.Click
        Windows.Forms.Cursor.Current = Cursors.WaitCursor
        Try
            If SearchText.Text.Length > 0 Then
                lblMsg.Text = ""
                ResultsListBox.Items.Clear()
                Dim refs As List(Of TaxonNamesInterface.Reference) = NamesClient.ReferencesInterfaceProxy.SearchReferences(SearchText.Text, MaxResults)
                If refs IsNot Nothing Then
                    For Each r As TaxonNamesInterface.Reference In refs
                        Dim citation As String = r.Citation
                        If citation Is Nothing OrElse citation.Length = 0 Then
                            citation = r.FullCitation
                        End If
                        ResultsListBox.Items.Add(New ResultItem(r.ReferenceId, citation))
                    Next
                    If refs.Count > MaxResults - 1 Then
                        lblMsg.Text = "> " + MaxResults.ToString + " references found"
                    Else
                        lblMsg.Text = (refs.Count - 1).ToString + " references found"
                    End If
                Else
                    lblMsg.Text = "No references found"
                End If
            Else
                Beep()
            End If
        Catch ex As Exception
            NamesException.LogError(ex)
            MsgBox("Error : " + ex.Message)
        End Try
        Windows.Forms.Cursor.Current = Cursors.Default
    End Sub

    Private Sub ResultsListBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ResultsListBox.SelectedIndexChanged
        RaiseEvent ReferenceSelected(ResultsListBox.SelectedItem)
    End Sub

    Private Sub SearchText_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles SearchText.KeyPress
        If e.KeyChar = Chr(13) Then
            SearchButton_Click(Me, EventArgs.Empty)
        End If
    End Sub

    Private Sub ReferenceSearch_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.GotFocus
        SearchText.Focus()
    End Sub
End Class

Public Class ResultItem
    Public Id As Guid = Guid.Empty
    Public Text As String

    Public Sub New()
    End Sub

    Public Sub New(ByVal itemId As Guid, ByVal itemTxt As String)
        Id = itemId
        Text = itemTxt
    End Sub

    Public Overrides Function ToString() As String
        Return Text
    End Function
End Class
